package views.games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import models.games.LuceRiflessa;
import views.util.GameColors;

public class LuceRiflessaView extends GameView {

	private JLabel[][] cells;
	private final int ROWS = 8;
	private final int COLUMNS = 11;
	private JPanel main;
	private JPanel tab;
	private ImageIcon slash;
	private ImageIcon backSlash;
	private GridBagConstraints gbc;
	private ActionListener currentActionListener;

	public LuceRiflessaView() {
		super(GameColors.VERDE, GameColors.VERDE_CHIARO, "TURNO 1 su 15",
				"PUNTEGGIO: 0", "");
		main = new JPanel();
		main.setBackground(GameColors.VERDE);
		gbc = new GridBagConstraints();
		main.setLayout(new GridBagLayout());
		add(main, BorderLayout.CENTER);
		tab = newTab();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.insets = new Insets(50, 50, 50, 50);
		main.add(tab, gbc);
		// int h = tab.getHeight();
		// int w = tab.getWidth();
		// Image scaledSlash = new ImageIcon("resources\\slash.png").getImage();
		// Image scaledBackSlash = new
		// ImageIcon("C:\\Users\\Meteo02\\Pictures\\PHOTOSHOP\\M-Health\\backSlash.png").getImage();
		// BufferedImage bi = new BufferedImage(scaledSlash.getWidth(null),
		// scaledSlash.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		// Graphics g = bi.createGraphics();
		// g.drawImage(scaledSlash, 0, 0, w, h, null, null);
		// slash = new ImageIcon(bi);
		// Image newimg = scaledSlash.getScaledInstance(cells[0][0].getWidth(),
		// cells[0][0].getHeight(), java.awt.Image.SCALE_SMOOTH);
		slash = new ImageIcon("resources\\luceRiflessa\\slash.png");
		backSlash = new ImageIcon("resources\\luceRiflessa\\backSlash.png");
	}

	/**
	 * @param dim
	 */
	// public void update( ){
	// tab.setVisible(false);
	// main.remove(tab);
	// tab = newTab();
	// main.add(tab, gbc);
	// }

	public JLabel[][] getCells() {
		return cells;
	}

	public ImageIcon getSlashImage() {
		return slash;
	}

	// public int getDimension() {
	// return dimension;
	// }
	//
	// public void setDimension(int d) {
	// dimension = d;
	// }

	public JLabel getRounds() {
		return gameClock;
	}

	public JLabel getScore() {
		return score;
	}

	public JLabel getTiles() {
		return other;
	}

	public void drawSlash(int x, int y) {
		if (x < 0 || x >= cells.length || y < 0 || y >= cells[0].length)
			throw new ArrayIndexOutOfBoundsException();
		cells[x][y].setIcon(slash);
	}

	public void drawBackSlash(int x, int y) {
		if (x < 0 || x >= cells.length || y < 0 || y >= cells[0].length)
			throw new ArrayIndexOutOfBoundsException();
		cells[x][y].setIcon(backSlash);
	}

	public void setCellColor(int x, int y, Color c) {
		if (x < 0 || x >= cells.length || y < 0 || y >= cells[0].length)
			throw new ArrayIndexOutOfBoundsException();
		cells[x][y].setBackground(c);
	}

	public void removeImage(int x, int y) {
		if (x < 0 || x >= cells.length || y < 0 || y >= cells[0].length)
			throw new ArrayIndexOutOfBoundsException();
		cells[x][y].setIcon(new ImageIcon());
	}

	public void drawStartingDirection(int x, int y, LuceRiflessa.Direction dir) {
		if (x < 0 || x >= cells.length || y < 0 || y >= cells[0].length)
			throw new ArrayIndexOutOfBoundsException();
		switch (dir) {
		case LEFT:
			cells[x][y].setIcon(new ImageIcon(
					"resources\\luceRiflessa\\left.png"));
			break;
		case RIGHT:
			cells[x][y].setIcon(new ImageIcon(
					"resources\\luceRiflessa\\right.png"));
			break;
		case UP:
			cells[x][y]
					.setIcon(new ImageIcon("resources\\luceRiflessa\\up.png"));
			break;
		case DOWN:
			cells[x][y].setIcon(new ImageIcon(
					"resources\\luceRiflessa\\down.png"));
			break;
		}
	}

	private JPanel newTab() {
		JPanel tab = new JPanel();
		cells = new JLabel[ROWS][COLUMNS];
		tab.setLayout(new GridLayout(ROWS, COLUMNS, 3, 3));
		tab.setBackground(GameColors.VERDE);
		for (int i = 0; i < ROWS; i++)
			for (int j = 0; j < COLUMNS; j++) {
				cells[i][j] = new JLabel(new ImageIcon());
				cells[i][j].setOpaque(true);
				Font f = new Font("Segoe UI", Font.BOLD, 18);// ONLY TEST
				cells[i][j].setFont(f);// ONLY TEST
				cells[i][j].setName(i + "-" + j);
				// if ( j == i/2)
				// cells[i][j].setIcon(slash);
				// else
				cells[i][j].setBackground(Color.white);
				cells[i][j].setName(i + "," + j);
				tab.add(cells[i][j]);
			}
		return tab;
	}// littleTab

	public void addMouseListenerToTheBorder(MouseListener a) {
		for (int j = 0; j < COLUMNS; j++) {
			cells[0][j].addMouseListener(a);
			cells[ROWS - 1][j].addMouseListener(a);
		}
		for (int i = 1; i < ROWS - 1; i++) {
			cells[i][0].addMouseListener(a);
			cells[i][COLUMNS - 1].addMouseListener(a);
		}
	}

	public void addActionListenerToGame(ActionListener al) {
		currentActionListener = al;
	}

	public void disableBorder() {
		for (int j = 0; j < COLUMNS; j++) {
			cells[0][j].setEnabled(false);
			cells[ROWS - 1][j].setEnabled(false);
		}
		for (int i = 1; i < ROWS - 1; i++) {
			cells[i][0].setEnabled(false);
			cells[i][COLUMNS - 1].setEnabled(false);
		}
	}

	public void enableBorder() {
		for (int j = 0; j < COLUMNS; j++) {
			cells[0][j].setEnabled(true);
			cells[ROWS - 1][j].setEnabled(true);
		}
		for (int i = 1; i < ROWS - 1; i++) {
			cells[i][0].setEnabled(true);
			cells[i][COLUMNS - 1].setEnabled(true);
		}
	}

	@Override
	public void gameEnded() {
		other.setVisible(false);
		// score.setVisible(false);
		gameClock.setVisible(false);
		tab.setVisible(false);
		main.remove(tab);
		gbc = new GridBagConstraints();
		JLabel end = newLabel("Partita Conclusa!", GameColors.VERDE_CHIARO);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.fill = gbc.HORIZONTAL;
		main.add(end, gbc);
		gbc.gridy = 1;
		main.add(score, gbc);
		JButton restart = newButton("Ricomincia");
		JButton menu = newButton("Menu Giochi");
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		main.add(restart, gbc);
		gbc.gridx = 2;
		main.add(menu, gbc);
		restart.addActionListener(currentActionListener);
		menu.addActionListener(currentActionListener);
	}

}
